﻿/*-----------------------------------------------------------------------
 * <copyright file="DataExtractionContainer.xaml.cs" company="O-RID">
 *     Copyright (c)2010 O-RID Co.,Ltd.
 * </copyright>
 * <author>Zhang jun</author>
 * <summary> 
 *          DataExtractionContainer
 *          数据抽取 公用查询框
 *          
 *          作成日：11/09/2010
 *          
 * </summary>
 *-----------------------------------------------------------------------
 */
namespace Orid.Management.Statistics.OperatorUnitMangement.Common.SearchCtrl
{
    using System;
    using System.Text;
    using System.Windows;
    using System.Windows.Controls;
    using Orid.Management.Statistics.OperatorUnitMangement.Common.FunctionList;
    using Orid.Management.Statistics.OperatorUnitMangement.WCFServers.OthersQueryRef;
    using childWindow = Orid.Management.Statistics.OperatorUnitMangement.Common.Window;
    using Wcf = Orid.Management.Statistics.OperatorUnitMangement.Common.Service;

    /// <summary>
    /// 数据抽取 公用查询框
    /// </summary>
    public partial class DataExtractionContainer : UserControl
    {
        /// <summary>
        /// 当前查询框的名字（日志用）
        /// </summary>
        private string ctrlName = " 数据抽取 公用查询框 DataExtractionContainer ";
       
        /// <summary>
        /// 消息子窗体 errorMessbox
        /// </summary>
        private childWindow.MessageBox errorMessbox = null;

        /// <summary>
        /// 日志服务定义
        /// wcfLogClient
        /// </summary>
        private WCFLog4Ref.WCFLog4Client wcfLogClient = null;

        /// <summary>
        /// 数据抽取 查询
        /// </summary>
        private WCFTheOthersQueryClient wcfTheDataExtractionQueryClient = null;

        /// <summary>
        /// 查询命令 导出命令
        /// </summary>
        private Command.ICommand queryCcommand = null, exportComand = null;

        /// <summary>
        /// 记录用户查询条件
        /// </summary>
        public string WhereIntorLogs
        {
            get;
            set;
        }

        /// <summary>
        /// 构造函数
        /// </summary>
        public DataExtractionContainer()
        {
            InitializeComponent();
            // Grid引用主题
            ((App)Application.Current).SetTheme(LayoutRoot);

            //Accordion 的Border 控件 引用主题
            //因为主题控件中只能包含一个子控件
            ((App)Application.Current).SetTheme(bAccordionItemBorder);

            this.InitField();

            //查询按钮
            this.btnSearch.Click+=new RoutedEventHandler(btnSearch_Click); 

            //数据导出
            this.btnExport.Click += new RoutedEventHandler(btnExport_Click);

            // 开始日期(DatePicker)
            this.dpDateFrom.SelectedDateChanged +=new EventHandler<SelectionChangedEventArgs>(dpDateFrom_SelectedDateChanged);

            // 结束日期(DatePicker)
            this.dpDateTo.SelectedDateChanged +=new EventHandler<SelectionChangedEventArgs>(dpDateTo_SelectedDateChanged);

            //设置 查询界面 的 查询按钮 数据导出 初始化可用
            this.btnSearch.IsEnabled = true;
            this.btnExport.IsEnabled = true;

            this.dpDateTo.SelectedDate = DateTime.Now.Date;
        }

        /// <summary>
        /// 变量初始化方法  InitField
        /// </summary>
        private void InitField()
        {
            this.errorMessbox = new childWindow.MessageBox();
            this.wcfLogClient = Wcf.Service.Log4;
            this.wcfTheDataExtractionQueryClient = Wcf.Service.TheDataExtractionQuery;
        }

        /// <summary>
        /// Gets or sets vlaue of exportComand
        /// 导出命令 ExportComand
        /// </summary>
        public Command.ICommand ExportComand
        {
            get { return this.exportComand; }
            set { this.exportComand = value; }
        }

        /// <summary>
        /// Gets or sets value of queryCcommand
        /// 查询命令 QueryCcommand
        /// </summary>
        public Command.ICommand QueryCcommand
        {
            get { return this.queryCcommand; }
            set { this.queryCcommand = value; }
        }

        /// <summary>
        /// 导出按钮 点击事件 BtnExport_Click
        /// </summary>
        /// <param name="sender">Sender of object</param>
        /// <param name="e">e type of RoutedEventArgs</param>
        private void btnExport_Click(object sender, RoutedEventArgs e)
        {
            writeWhereLog();
            this.btnExport.IsEnabled = false;

            this.exportComand.Exec();
        }

        /// <summary>
        /// 查 询 按钮
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The instance containing the event data.</param>
        void btnSearch_Click(object sender, RoutedEventArgs e)
        {
            writeWhereLog();
            this.btnSearch.IsEnabled = false;

            this.queryCcommand.Exec();
        }

        #region 日期(DatePicker)
        /// <summary>
        /// 结束日期(DatePicker)
        /// DpDateTo_SelectedDateChanged
        /// </summary>
        /// <param name="sender">sender of obejct</param>
        /// <param name="e">e type of SelectionChangedEventArgs</param>
        private void dpDateTo_SelectedDateChanged(object sender, SelectionChangedEventArgs e)
        {
            if (dpDateTo == null || dpDateTo.SelectedDate == null)
            {
                return;
            }
            else
            {
                this.dpDateTo.Text = this.dpDateTo.SelectedDate.Value.ToString("yyyy-MM-dd");
            }
        }

        /// <summary>
        /// 开始日期(DatePicker)
        /// 防止选错时，出现BUG
        /// </summary>
        /// <param name="sender">sender of object</param>
        /// <param name="e">e type of SelectionChangedEventArgs</param>
        private void dpDateFrom_SelectedDateChanged(object sender, SelectionChangedEventArgs e)
        {
            if (dpDateFrom == null || dpDateFrom.SelectedDate == null)
            {
                return;
            }
            else
            {
                this.dpDateFrom.Text = this.dpDateFrom.SelectedDate.Value.ToString("yyyy-MM-dd");
            }
        }

        #endregion 日期(DatePicker)

        #region 记录用户选择的WHERE条件
        /// <summary>
        /// 记录用户选择的WHERE条件
        /// </summary>
        /// <returns></returns>
        /// <value>10/15/2010 Add </value>
        private void writeWhereLog()
        {
            StringBuilder sbWhereIntorLogs = new StringBuilder();
            try
            {
                //日志用
                sbWhereIntorLogs.Append(" 查询条件：");
                sbWhereIntorLogs.Append(" 开始日期：");
                sbWhereIntorLogs.Append(string.IsNullOrEmpty(this.dpDateFrom.Text) == true ? " null" : this.dpDateFrom.Text);
                sbWhereIntorLogs.Append(" 结束日期：");
                sbWhereIntorLogs.Append(string.IsNullOrEmpty(this.dpDateTo.Text) == true ? " null" : this.dpDateTo.Text);
                
                if (this.cbOverMark.Visibility == Visibility.Visible)
                {
                    sbWhereIntorLogs.Append(" オーダ状态：");
                    sbWhereIntorLogs.Append(string.IsNullOrEmpty((this.cbOverMark.SelectedItem as ComboBoxItem).Tag.ToString())
                                            == true ? " null" : (this.cbOverMark.SelectedItem as ComboBoxItem).Tag.ToString());
                }
            }
            
            catch
            {
                WhereIntorLogs = string.Empty;
            }

            WhereIntorLogs = sbWhereIntorLogs.ToString();
        }

        #endregion 记录用户选择的WHERE条件
    }
}
